from cfg.exts import db
import sqlalchemy as sql


class BasicInfo(db.Model):
    __tablename__ = "c_basic_info"
    id = sql.Column(sql.BIGINT, primary_key=True, autoincrement=True)
    note_id = sql.Column(sql.VARCHAR(256), nullable=True, unique=True)
    rank = sql.Column(sql.INT, nullable=True, doc="排名")
    title = sql.Column(sql.VARCHAR(1024), nullable=True, doc="排名")
    image = sql.Column(sql.VARCHAR(2048), nullable=True, doc="图片")
    category = sql.Column(sql.VARCHAR(256), nullable=False, doc="类别")
    subcategory = sql.Column(sql.VARCHAR(256), nullable=True, doc="子类别")
    author_name = sql.Column(sql.VARCHAR(128), nullable=True, doc="作者名")
    fans = sql.Column(sql.INT, nullable=True)
    interact = sql.Column(sql.INT, nullable=True)
    liked = sql.Column(sql.INT, nullable=True)
    collected = sql.Column(sql.INT, nullable=True)
    comments = sql.Column(sql.INT, nullable=True)
    shared = sql.Column(sql.INT, nullable=True)
    source = sql.Column(sql.INT, nullable=True)
    content = sql.Column(sql.TEXT, nullable=True)
    create_time = sql.Column(sql.TIMESTAMP, nullable=True, server_default=sql.func.now())
    isLowFan = sql.Column(sql.INT, nullable=True, server_default=1, doc="是否低粉报文，1为是，0为否")

    @classmethod
    def build(cls, json, source, content, image):
        target = 0
        if source.__eq__("chan"):
            target = 0
        elif source.__eq__("douyin"):
            target = 1
        return BasicInfo(
            note_id=json.get("note_id"),
            rank=json.get("rank"),
            title=json.get("title"),
            category=json.get("category"),
            subcategory=json.get("subcategory"),
            author_name=json.get('author_nickname'),
            fans=json.get("fans"),
            interact=json.get("interact"),
            liked=json.get("liked"),
            collected=json.get("collected"),
            comments=json.get("comments"),
            shared=json.get("shared"),
            content=content,
            source=target,
            image=image
        )

    def toStr(self):
        return {
            "id": self.id,
            "note_id": self.note_id,
            "rank": self.rank,
            "title": self.title,
            "category": self.category,
            "subcategory": self.subcategory,
            "author_name": self.author_name,
            "fans": self.fans,
            "interact": self.interact,
            "liked": self.liked,
            "collected": self.collected,
            "comments": self.comments,
            "shared": self.shared,
            "content": self.content,
            "source": self.source,
            "image": self.image,
        }
